//
//  GuideViewController.m
//  Arrow
//
//  Created by Li Chengliang on 13-3-14.
//  Copyright (c) 2013年 Li Chengliang. All rights reserved.
//

#import "GuideViewController.h"
#import "AppDelegate.h"
#import "Global.h"

@interface GuideViewController () <UIScrollViewDelegate>
{
    UIScrollView *_scrollView;
    NSInteger total;
}
@property(nonatomic, strong) UIPageControl *pageControl;
@end


@implementation GuideViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
        
    _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.showsVerticalScrollIndicator = NO;
    _scrollView.delegate = self;
    _scrollView.pagingEnabled = YES;
    [self.view insertSubview:_scrollView atIndex:0];

    total = 4 ;

    /*
    _pageControl = [[UIPageControl alloc] initWithFrame:RECTMAKE(100, IS_IPHONE5()?self.view.frame.size.height-45:self.view.frame.size.height-35, self.view.frame.size.width-200, 30)];
    _pageControl.numberOfPages = total;
    [_pageControl setCurrentPage:0];
    [_pageControl addTarget:self action:@selector(pageControlChanged:) forControlEvents:UIControlEventValueChanged];
    _pageControl.pageIndicatorTintColor=[UIColor whiteColor];
    _pageControl.currentPageIndicatorTintColor=KNEWGREENTEXTCOLOR;
    [self.view insertSubview:_pageControl atIndex:1];
     */
    
    for (int i=0; i < total; i++)
    {
        UIImage *image = nil;
        image = [UIImage imageNamed:[NSString stringWithFormat:@"guide%d", i+1]];
        
        UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
//        imageView.image = image;
        imageView.frame = CGRectMake(kScreenW*i, 0, kScreenW, self.view.frame.size.height);
        [_scrollView addSubview:imageView];
        if (i == total - 1)
        {
            UIButton *btnStart = [UIButton buttonWithType:UIButtonTypeCustom];
            [btnStart addTarget:self action:@selector(btnStartClicked) forControlEvents:UIControlEventTouchUpInside];
//            [btnStart setBackgroundImage:[UIImage imageNamed:@"btnEnter.png"]  forState:UIControlStateNormal];
            btnStart.adjustsImageWhenHighlighted = NO;
            btnStart.backgroundColor = KBACKNAVIGATIONCOLOR;
            btnStart.frame = CGRectMake((kScreenW-110)/2,self.view.frame.size.height-100, 110, 36);
            [btnStart setTitle:@"立即体验" forState:UIControlStateNormal];
            [btnStart setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            btnStart.clipsToBounds = YES;
            btnStart.layer.cornerRadius = 18;
            [imageView addSubview:btnStart];
            imageView.userInteractionEnabled = YES;
        }
    }
    _scrollView.contentSize = CGSizeMake(self.view.frame.size.width*total, self.view.frame.size.height);
}

//点击pageControl上的白色点触发的事件
-(void)pageControlChanged:(id)sender
{
    NSInteger page = _pageControl.currentPage;
    CGRect frame = _scrollView.frame;
    frame.origin.x = frame.size.width*page;
    frame.origin.y = 0;
    [_scrollView scrollRectToVisible:frame animated:NO];
}

- (void)btnStartClicked
{
    [[NSUserDefaults standardUserDefaults]setObject:IOSSHORTAPPVERSION forKey:GUIDE_VERSION];
    [[NSUserDefaults standardUserDefaults] synchronize];
    AppDelegate* appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
    [appDelegate applicationLaunched];
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat pageWidth = scrollView.frame.size.width;
    int page = floor((scrollView.contentOffset.x - pageWidth/2)/pageWidth)+1;
    [self.pageControl setCurrentPage:page];
    
    if (page == total - 1 && _scrollView.contentOffset.x>(total-1)*kScreenW)
    {
        [self btnStartClicked];
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
